<?php

class Twitter extends Plugins implements IPlugins {
	
	public $BASE_URL = "http://search.twitter.com/search.atom?";
	private $method;
	public $output = array();
		
	function __construct($query, $max_results) {
		/*
		* Add metadata to output array
		* and setup query
		*/
		$this->output["feed"] = "Twitter";
		$this->method = array(
				"q" => $query,
				"rpp" =>  $max_results
			);
	}
	
	function load() {

		/**
		* Format querystring & fetch feed
		*/
		$query = parent::query_params($this->method);
		$xml = parent::load_xml($this->BASE_URL, $query);

		$feed = array();
	
		/**
		* Set up items
		*/
		foreach ($xml->entry as $entry)
		{	
		
			$item = array();
			
			$item["network"] = "twitter";
			$item["id"] = (string) $entry->id;
			$item["published"] = (string) $entry->published;
			$item["title"] = (string) $entry->title;
			$item["author"] = (string) $entry->author->name;
			$item["url"] = (string) $entry->uri;
			$item["permalink"] = (string) $entry->link[0]["href"];
			$item["profile_image"] = (string) $entry->link[1]["href"];
			
			array_push($feed, $item);
			
			/**
			* Create a "limited" general object to access everything in the feed.
			*/
			parent::create_socialmash_object($item);
			
		}
			
		$this->output["items"] = $feed;
		
	}

	function render()
	{
		echo(json_encode($this->output));
	}
	
	function toJson()
	{
		parent::toJson($this->output);
	}
	
	function toString() {
		return parent::toString();
	}

	
}

?>